package com.school.admin.controller;


import com.school.admin.common.annotation.IgnoreSecurity;
import com.school.admin.common.result.R;
import com.school.generator.bean.BaseBean;
import com.school.generator.service.RawDataService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.util.zip.ZipOutputStream;

/**
 * @program: humble_boot
 * @description: 代码生成器
 * @author: sunpx
 * @create: 2022-04-26 21:13
 **/
@RestController
@RequestMapping("/codeGenerator")
@Api(tags = "代码生成器")
public class CodeGeneratorController {

    @Autowired
    RawDataService rowDataService;

    @GetMapping("tableList/{dataBase}")
    @IgnoreSecurity
    public R tableList(@PathVariable("dataBase") String dataBase) {
        return R.ok().data("list", rowDataService.tableList(dataBase));
    }


    @GetMapping("getByTableFieldList/{tableName}")
    @IgnoreSecurity
    public R getByTableFieldList(@PathVariable("tableName") String tableName) {
        return R.ok().data("info", rowDataService.getBaseBean(tableName));
    }


    @PostMapping("codeGenerator")
    public R codeGenerator(HttpServletResponse httpServletResponse, @RequestBody BaseBean baseBean) throws Exception {
        baseBean.setAuthor("sunpx")
                .setPackagePath("com.school.admin");
        ServletOutputStream outputStream = httpServletResponse.getOutputStream();
        ZipOutputStream zipOutputStream = new ZipOutputStream(outputStream);
        rowDataService.generateProjectFiles(baseBean, zipOutputStream);
        zipOutputStream.close();
        outputStream.close();
        return R.ok();
    }

}
